<template>
  <div>
    <el-form inline>

      <!-- 开始时间 -->
      <el-form-item label="开始时间">
        <el-date-picker
            size="mini"
            style="width: 175px"
            v-model="startTime"
            type="datetime"
            placeholder="选择开始时间"
            default-time="12:00:00"
            :picker-options="{start:'00:00:00',end:'23:59:59'}"
            @change="selectChange(1)"
        >
        </el-date-picker>
      </el-form-item>

      <!-- 结束时间 -->
      <el-form-item label="结束时间">
        <el-date-picker
            size="mini"
            style="width: 175px"
            v-model="endTime"
            type="datetime"
            placeholder="选择结束时间"
            default-time="12:00:00"
            :picker-options="{start:'00:00:00',end:'23:59:59'}"
            @change="selectChange(2)"
        >
        </el-date-picker>
      </el-form-item>

      <el-button
          @click="workPath"
          type="primary"
          size="mini">查看路劲
      </el-button>

    </el-form>
  </div>
</template>

<script>

export default {
  data() {
    return {
      //  ----------- 查询表单 --------------
      startTime: undefined,
      endTime: undefined,
    }
  },
  methods: {
    // -------------- 查询功能 --------------

    /**
     * 查询条件发送变化
     * */
    selectChange: function (index) {
      if (this.startTime === undefined || this.endTime === undefined) {
        return 0
      }
      let s_obj = new Date(this.startTime);
      let e_obj = new Date(this.endTime);
      if (s_obj.getTime() >= e_obj.getTime()) {
        this.$message("开始时间必须早于结束时间!");
        switch (index) {
          case 1:
            this.startTime = undefined;
            break;
          case 2:
            this.endTime = undefined;
            break;
          default:
            break;
        }
        return 0
      }
    },

    /**
     * 获取工作路径
     * */
    workPath: function () {
      this.$emit("workPath", [this.s_Time, this.e_Time]);
    },
  },
  computed: {
    s_Time: function () {
      let date = new Date(this.startTime)
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
    },
    e_Time: function () {
      let date = new Date(this.endTime)
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
    },
  }
}
</script>

<style>

.el-form {
  text-align: left;
}

.el-form-item {
  margin-left: 1.5vw;
}
</style>